// MainDlg.cpp : implementation of the CMainDlg class
//
/////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "resource.h"

#include "aboutdlg.h"
#include "MainDlg.h"


#define CONTENTS_LENGTH	(2048)
typedef struct
{
	unsigned char	cTraceType;
	unsigned char	cWinNum;
	unsigned char	cTypeNum;
	unsigned char	cReserve;
	char			pContents[CONTENTS_LENGTH];
} AXIATRACEDATA, FAR *LPAXIATRACEDATA;


BOOL CMainDlg::PreTranslateMessage(MSG* pMsg)
{
	return CWindow::IsDialogMessage(pMsg);
}

BOOL CMainDlg::OnIdle()
{
	return FALSE;
}

LRESULT CMainDlg::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	// center the dialog on the screen
	CenterWindow();

	// set icons
	HICON hIcon = (HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDR_MAINFRAME), 
		IMAGE_ICON, ::GetSystemMetrics(SM_CXICON), ::GetSystemMetrics(SM_CYICON), LR_DEFAULTCOLOR);
	SetIcon(hIcon, TRUE);
	HICON hIconSmall = (HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDR_MAINFRAME), 
		IMAGE_ICON, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR);
	SetIcon(hIconSmall, FALSE);

	rich_edit_ctrl_ = GetDlgItem(IDC_RICHEDIT21);

	//for (int i=0; i<100; ++i) {
	//	rich_edit_ctrl_.AppendText("12345\n");
	//	rich_edit_ctrl_.PostMessage(WM_VSCROLL, SB_BOTTOM,0); 
	//}
	// register object for message filtering and idle updates
	CMessageLoop* pLoop = _Module.GetMessageLoop();
	ATLASSERT(pLoop != NULL);
	pLoop->AddMessageFilter(this);
	pLoop->AddIdleHandler(this);

	UIAddChildWindowContainer(m_hWnd);

	return TRUE;
}

LRESULT CMainDlg::OnDestroy(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	// unregister message filtering and idle updates
	CMessageLoop* pLoop = _Module.GetMessageLoop();
	ATLASSERT(pLoop != NULL);
	pLoop->RemoveMessageFilter(this);
	pLoop->RemoveIdleHandler(this);

	return 0;
}

LRESULT CMainDlg::OnAppAbout(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	CAboutDlg dlg;
	dlg.DoModal();
	return 0;
}

LRESULT CMainDlg::OnOK(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	// TODO: Add validation code 
	CloseDialog(wID);
	return 0;
}

LRESULT CMainDlg::OnCancel(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	CloseDialog(wID);
	return 0;
}

void CMainDlg::CloseDialog(int nVal)
{
	DestroyWindow();
	::PostQuitMessage(nVal);
}


LRESULT  CMainDlg::OnCopyData(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {

	COPYDATASTRUCT* copy_data = (COPYDATASTRUCT*)(lParam);
	if (!copy_data) {
		return 0;
	}

	AXIATRACEDATA* data = (AXIATRACEDATA*)(copy_data->lpData);
	rich_edit_ctrl_.SetSel(-1, -1);
	rich_edit_ctrl_.ReplaceSel( (LPCTSTR)data->pContents );
	rich_edit_ctrl_.AppendText("\n");
	rich_edit_ctrl_.PostMessage(WM_VSCROLL, SB_BOTTOM,0); 

	return 0;
}
